/*******************************************************************************
 * Licensed Materials - Property of IBM
 * (c) Copyright IBM Corporation 2005, 2008. All Rights Reserved. 
 * 
 * Note to U.S. Government Users Restricted Rights:  Use, 
 * duplication or disclosure restricted by GSA ADP Schedule 
 * Contract with IBM Corp.
 *******************************************************************************/

package org.eclipsecon.dui.interpreter.internal;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.util.URI;
import org.eclipse.ui.progress.UIJob;
import org.eclipsecon.dui.interpreter.internal.ModelContainer.Target;

public class ReloadJob extends UIJob {
	private final URI fModelPath;
	private final ModelContainer fModelContainer;
	private final IPath script;

	public ReloadJob(ModelContainer modelContainer, URI modelPath, IPath script) {
		super("Redraw");
		fModelContainer= modelContainer;
		fModelPath= modelPath;
		this.script = script;
	}

	/*
	 * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public IStatus runInUIThread(IProgressMonitor monitor) {

		/* Reload UI, Binding and Data */
		if (fModelContainer.getBindingPath() != null && fModelContainer.getDataPath() != null && fModelContainer.getTarget() == Target.SWT) {
			IStatus status= fModelContainer.load(fModelPath, script);

			if (status.getCode() != IStatus.OK)
				return status;

			return new BindingJob(fModelContainer, fModelContainer.getContext(), fModelContainer.getBindingPath(), fModelContainer.getDataPath()).runInUIThread(monitor);
		}

		/* Only reload UI */
		return fModelContainer.load(fModelPath, script);
	}
}